
local ChooseSceneCCB = class("ChooseSceneCCB", cc.load("mvc").ViewBase)


function ChooseSceneCCB:ctor(args)
    args = args or {}
    self.showId = args.showId
    _init(self)

    ChooseSceneCCB.super.ctor(self)
end

_init = function(self)
   	self.luafilePath = "views.ChooseSceneCCB"
   	self:getCcbNode():addTo(self)
	  local characterDataForChoose = ManagerUserData.userData.characterDataForChoose or {}
	  local num = #characterDataForChoose
   	for i=1, 5 do
   			if i <= num then
   				_showCharacterInfo(self, i)
   			else
   				self["controlButtton"..i]:removeFromParent()
   			end
   	end

 		self.currentId = self.showId or 1 

   	_showCharacterAni(self, self.currentId)

    self.setControlButtonTitle(self.controlButton1, Word(0).ChooseSceneCCB_01)
    self.setControlButtonTitle(self.controlButton2, Word(0).ChooseSceneCCB_02)
    self.setControlButtonTitle(self.controlButton3, Word(0).ChooseSceneCCB_03)
end

function ChooseSceneCCB:onEnter_()
    ManagerNotification:registerObserver("user.deleteCharacter",self,handler(self, _onDelete))
    ManagerNotification:registerObserver("user.enterGame",self,_onEnterGame )

end

function ChooseSceneCCB:onExit_()
    ChooseSceneCCB.super.onExit_()
    ManagerNotification:unregisterObserver("user.deleteCharacter",self)
    ManagerNotification:unregisterObserver("user.enterGame",self)

end

function ChooseSceneCCB.createCall()
    MyApp:enterScene("CreateSceneCCB")
end

function ChooseSceneCCB.call1(sender,controlEvent,self)
    _showCharacterAni(self,1) 
end

function ChooseSceneCCB.call2(sender,controlEvent,self)
    _showCharacterAni(self,2) 
end

function ChooseSceneCCB.call3(sender,controlEvent,self)
    _showCharacterAni(self,3) 
end

function ChooseSceneCCB.call4(sender,controlEvent,self)
    _showCharacterAni(self,4) 
end

function ChooseSceneCCB.call5(sender,controlEvent,self)
    _showCharacterAni(self,5) 
end

function ChooseSceneCCB.enterCall(sender,controlEvent,self)
    AdapterSendMsg:sendMsg("user.enterGame",{characterId = self.currentId})
end

function ChooseSceneCCB.deleteCall(sender,controlEvent,self)
    AdapterSendMsg:sendMsg("user.deleteCharacter",{characterId = self.currentId})
end

_onDelete = function(self, characterList)
    local num = #characterList
    local showId
    if self.currentId > num then 
      showId = self.currentId - 1
    else
      showId = self.currentId
    end

    MyApp:enterScene("ChooseSceneCCB", {showId = showId})

end

_showCharacterAni = function(self, id)
    local characterInfo = ManagerUserData.userData.characterDataForChoose[id]
    if characterInfo then
      self.modelNode:removeAllChildren()
      local modelId = characterInfo.modelId
      local ccbNode = ManagerRes.getCharacterCCBNode("big_" .. modelId)
      ccbNode:addTo(self.modelNode)
      self.currentId = id
    end
end

_showCharacterInfo = function(self, id)
	
	local info = ManagerUserData.userData.characterDataForChoose[id]
	local jobs = {Word(0).job_01,Word(0).job_02,Word(0).job_03}

	local name = info.name
	local job = jobs[info.job]
	local level = info.level
	self["nameTxt" .. id]:setString(name)
	self["levelTxt" .. id]:setString(Word(0).level .. ":" .. level .. "\n" .. job)

end


_onEnterGame = function()
   
    MyApp:enterScene("MainSceneCCB")
   
end


return ChooseSceneCCB
